// pages/device/device.ts
const app = getApp<IAppOption>()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    snCode:"",
    sysData:{},
    userInfo:{},
    splits:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var me = this;
    try {
      me.setData({
        snCode: options.snCode,
        sysData:app.globalData.sysData,
        userInfo: app.globalData.userInfo,
        device:{
          info:{}
        }
      },()=>{
        me.refresh();
      })
    } catch (error) {
      console.log(error);
    }
  },
  refresh() {
    var me = this;
    try {
      var ps = {
        snCode : me.data.snCode
      };
      var device={};
      app.api.callApi('custom/getDeviceData' , ps).then(res=>{
        device= res[0]; 
        device.info ={};
        me.setData({
          device:device
        },()=>{
          wx.showLoading({
            title: '加载中',
          })
          app.api.getDeviceInfo(me.data.snCode).then(_info=>{
            wx.hideLoading();
            device.info = _info;
            me.refreshSplits();
            me.setData({
              device:device
            },()=>{
              console.log(me.data)
            })
          })
        })
      })
    } catch (error) {
      console.log(error);
    } 
  },
  refreshReal() {
    var me = this;
    try {
      var device = me.data.device;
      app.api.getDeviceInfo(me.data.snCode).then(_info=>{
        device.info = _info;
        me.setData({
          device:device
        },()=>{
          console.log(me.data)
        })
      })
    } catch (error) {
      console.log(error);
    } 
  },
  refreshSplits() {
    var me = this;
    try { 
      app.api.getDeviceSplit(me.data.snCode).then(_splits=>{
        me.setData({
          splits:_splits
        }, ()=>{
          console.log(me.data)
        })
      })
    } catch (error) {
      console.log(error);
    } 
  },
  acOpenLed() {
    var me = this;
    try { 
      var ps = {
        snCode: me.data.snCode ,
        light:"1"
      }; 
      app.api.callDeviceApi('setLightStatus',ps).then(res=>{   
        console.log('操作指令完成：',res)         
        wx.showToast({
          title: '正在获取设备状态...',
          icon: 'success',
          duration: 8000
        });
        setTimeout(()=>{
          me.refreshReal();
        },5000);
      }) 
    } catch (error) {
      console.log(error);
    } 
  },
  acCloseLed() {
    var me = this;
    try { 
      var ps = {
        snCode: me.data.snCode ,
        light:"0"
      }; 
      app.api.callDeviceApi('setLightStatus',ps).then(res=>{   
        console.log('操作指令完成：',res)         
        wx.showToast({
          title: '正在获取设备状态...',
          icon: 'success',
          duration: 8000
        });
        setTimeout(()=>{
          me.refreshReal();
        },5000);
      }) 
    } catch (error) {
      console.log(error);
    } 
  },
  acInput() {
    var me = this;
    try { 
      app.api.callDeviceApi('getRealData',{snCode: me.data.snCode}).then(res=>{  
        wx.showToast({
          title: '正在获取设备状态...',
          icon: 'success',
          duration: 3000
        });
        me.refreshReal();
      }) 
    } catch (error) {
      console.log(error);
    } 
  },
  acTakePhoto() {
    var me = this;
    try { 
      app.api.callDeviceApi('getNewImage',{snCode: me.data.snCode  }).then(res=>{            
        wx.showToast({ 
          title: '已下发拍照指令，约20秒后重新刷新页面...',
          icon: 'success',
          duration: 20000
        });
        setTimeout(()=>{
          me.refreshReal();
        },20000);
      })
    } catch (error) {
      console.log(error);
    } 
  },
  acRestart() {
    var me = this;
    try { 
      app.api.callDeviceApi('issueOrder',{snCode: me.data.snCode ,cmd: 'reboot' }).then(res=>{            
        wx.showToast({
          title: '已下发拍照指令，约20秒后重新刷新页面...',
          icon: 'success',
          duration: 20000
        });
        setTimeout(()=>{
          me.refreshReal();
        },20000);
      })
    } catch (error) {
      console.log(error);
    } 
  },
  acPreview(en){
    var me = this;
    try {
      wx.previewImage({
        urls:[me.data.device.info.imageUrl]
      })
    } catch (error) {
      console.log(error)
    }
  }, 
  switchSplitItem(ev) {
    var me = this;
    try { 
      console.log(ev.currentTarget.dataset.ind);
      var ind = ev.currentTarget.dataset.ind;
      var splits = me.data.splits;
      var flag = splits[ind].f;
      splits[ind].f = !flag;
      me.setData({
        splits:splits
      },()=>{
        var newSplits=[]; 
        var current =null;
        for( var i=0;i<splits.length;i++){
          var item = splits[i];
          if( item.f){
            if( current==null){
              current={
                start:i*3600,
                len:3600
              }
            }
            else{
              current.len+=3600;
            }
            if( i==splits.length-1){
              newSplits.push( JSON.parse(JSON.stringify(current)))
            }
          }
          else{
            if( current!=null){
              newSplits.push( JSON.parse(JSON.stringify(current)));
              current = null;
            }
          }
        }
        app.api.setDeviceSplit(me.data.snCode, newSplits).then(_res=>{
          console.log(_res);
        })
        console.log(newSplits)
      })

    } catch (error) {
      console.log(error);
    } 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})